import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import {noticeStatus, noticeImportant} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import TagSelect from 'Shared/components/TagSelect';
import {hasPermissions} from './utils';
import routes from './routes';
import {injectIntl, language} from './intl';
import DateRangePicker from 'Shared/components/DateRangePicker';
import WrappedSelect from '../common/WrappedSelect';
import TextArea from 'Shared/components/TextArea';
const FormItem = Form.Item;
const noticeStatusList = noticeStatus.toList(language);
const noticeImportantList = noticeImportant.toList(language);
class QueryPanel extends React.PureComponent {
    state = {
        data: {}
    }

    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.noticeProgram) && !this.props.isDealerUser)
            operations.push(<Button key="noticeProgram" href={`#${routes.noticeProgram.url()}`}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.noticeProgram',
                defaultMessage: '栏目管理'
            })}</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.signinStatistics))
            operations.push(<Button key="signinStatistics" href={`#${routes.signinStatistics.url()}`}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.signinStatistics',
                defaultMessage: '签收回复统计'
            })}</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.add) && !this.props.isDealerUser)
            operations.push(<Button key="add" type="primary" href={`#${routes.add.url()}`}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.add',
                defaultMessage: '新增'
            })}</Button>);
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.noticeTime',
                                defaultMessage: '发布时间'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DateRangePicker
                                name="noticeTime"
                                value={this.props.conditions.noticeTime}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.noticerName',
                                defaultMessage: '发布人'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="noticerName"
                                value={this.props.conditions.noticerName}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.program',
                                defaultMessage: '栏目'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="programId"
                                value={this.props.conditions.programId}
                                options={this.props.initEnum.programs}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.name',
                                defaultMessage: '主题'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="name"
                                value={this.props.conditions.name}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.important',
                                defaultMessage: '重要度'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="important"
                                value={this.props.conditions.important}
                                options={noticeImportantList}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    {!this.props.isDealerUser &&
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerCode',
                                defaultMessage: '服务站编号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextArea
                                name="dealerCode"
                                placeholder={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerCode.placeholder',
                                    defaultMessage: '最多输入1000个，以回车隔开'
                                })}
                                value={this.props.conditions.dealerCode}
                                onBlur={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    }
                </Row>
                {!this.props.isDealerUser &&
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={this.props.intl.formatMessage({
                            id: 'queryPanel.form.status',
                            defaultMessage: '状态'
                        })} {...FORM_OPTIONS.item}>
                            <TagSelect
                                name="status"
                                value={this.props.conditions.status}
                                options={noticeStatusList}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                </Row>
                }
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {operations}
                    </Col>
                </Row>
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getInitData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const programs = initData.programs ? initData.programs.map(item => ({
            text: `${item.name}`,
            value: item.id
        })) : [];
        return {
            programs
        };
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    conditions: getConditions(state),
    initEnum: getInitData(state),
    permissions: getPermissions(state),
    isDealerUser: getDealerUser(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
